<?php
namespace app\member\pay;
/**
 * 支付宝移动端服务
 */
class AlipayMobilePay extends \app\base\service\BaseService {

    private $rsa = '';

    public function __construct() {
        $this->rsa = ROOT_PATH . 'public/pay/alipay_mobile.pem';
    }

    public function getUrl($data, $returnUrl) {

        if (empty($data)) {
            return $this->error('订单数据未提交!');
        }
        $config = target('member/PayConfig')->getConfig('alipay_mobile');
        if (empty($config['partner']) || empty($config['key'])) {
            return $this->error('请先配置支付接口信息!');
        }

        $notifyUrl = DOMAIN . url('api/member/AlipayMobile/index');
        $returnUrl = DOMAIN . $returnUrl;
        $config = [
            'partner' => $config['partner'],// 请填写自己的支付宝账号信息
            'md5_key' => $config['key'],// 此密码无效，请填写自己对应设置的值
            'rsa_private_key' => $this->rsa,
            "notify_url" => $notifyUrl,
            "return_url" => $returnUrl,
        ];

        if (!is_file($config['rsa_private_key'])) {
            return $this->error('请先上传rsa私钥!');
        }

        $ip = \dux\lib\Client::getUserIp();

        $payData = [
            'order_no' => $data['order_no'],
            'amount' => $data['money'] ? price_format($data['money']) : 0,
            'client_ip' => ($ip == '::1') ?  '127.0.0.1' : $ip,
            'subject' => str_len($data['title'], 125),
            'body' => $data['body'] ? $data['body'] : $data['title'],
            'show_url' => DOMAIN . $data['url'],
            'extra_param' => $data['app'],
        ];

        if (empty($payData['order_no'])) {
            return $this->error('订单号不能为空!');
        }
        if ($payData['amount'] <= 0) {
            return $this->error('支付金额不正确!');
        }
        if (empty($payData['subject']) || empty($payData['body'])) {
            return $this->error('支付信息描述不正确!');
        }
        if (empty($payData['show_url'])) {
            return $this->error('商品展示页不正确!');
        }
        if (empty($payData['extra_param'])) {
            return $this->error('订单应用名不正确!');
        }
        try {
            $charge = new \Payment\ChargeContext();
            $type = \Payment\Config::ALI_CHANNEL_WAP;
            $charge->initCharge($type, $config);
            $url = $charge->charge($payData);
            return $this->success($url);
        } catch (\Payment\Common\PayException $e) {
            return $this->error($e->errorMessage());
        }
    }

    public function notifyPay($class) {
        if (!is_object($class)) {
            return $this->error('通知类错误!');
        }
        $config = target('member/PayConfig')->getConfig('alipay_mobile');
        if (empty($config['partner']) || empty($config['key'])) {
            return $this->error('请先配置支付接口信息!');
        }

        $config = [
            'partner' => $config['partner'],
            'md5_key' => $config['key'],
            'rsa_private_key' => $this->rsa
        ];
        $notify = new \Payment\NotifyContext();
        $callback = $class;
        try {
            $notify->initNotify(\Payment\Config::ALI, $config);
            return $this->success($notify->notify($callback));
        } catch (PayException $e) {
            return $this->error($e->errorMessage());
        }
    }


}